package org.ubc.cs410.disposal2020.server;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ubc.cs410.disposal2020.Game;
import org.ubc.cs410.disposal2020.GameFactory;
import org.ubc.cs410.disposal2020.NoActiveGameException;
import org.ubc.cs410.disposal2020.PMF;

/*
 * Implementation for the "admin tab" link
 */

public class AdminServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException { 

		// Template data
		Map<String, Object> root = new HashMap<String, Object>();

		// Page title
		root.put("title", "Admin Panel");


		String action = req.getParameter("act");
		if (action!=null && action.equalsIgnoreCase("clearGames")) {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				Query query = pm.newQuery(Game.class);
				query.deletePersistentAll();
			} finally {
				root.put("result", "Cleared all games.");
				pm.close();
			}
		} else {
			root.put("result", " ");
		}

		// Do the rendering
		ServletUtils.renderTemplate(getServletContext(), root, "Admin.ftl",
				resp, req.getRequestURI());
	}
}
